package cn.nit.algorithm.experiment;

/**
 * @author LXQ
 * @version 1.0
 * @date 2021/6/3 18:58
 */

import java.util.ArrayList;
import java.util.Random;

/**
 * 试验一：
 *
 *   有12个硬币，分别用A到L表示，其中恰好有一个假币，假币的重量不同于真币，所有真币的重量
 *   相同。现在采用天平称重的方式找到这个假币，某人已经给了一种3次称重的方案。编写一个程序
 *   找到这个假币。
 *
 */
public class Experiment5 {
    ArrayList<Coin> coins = new ArrayList();
    public String totalString = "";

    //设置硬币，并随机假币及重量
    public Experiment5() {
        Random random = new Random();
        int fakeCoin = random.nextInt(12);
        for (int i = 0; i < 12; i++) {
            Coin coin = new Coin();
            coin.name = String.valueOf((char) (65 + i));// + String.valueOf(i);
            coins.add(i, coin);
        }
        int newWeight = random.nextInt(2) == 0 ? -1 : 1;
        Coin c = coins.get(fakeCoin);
        c.weight += newWeight;
        coins.set(fakeCoin, c);
        for (Coin coin : coins) {
            System.out.print(coin);
            totalString+=coin;
        }
        System.out.println();
        totalString+="<br>";
    }

    private Integer weight(int... index) {
        int total = 0;
        for (int i : index) {
            total += coins.get(i).weight;
        }
        return total;
    }

    /**
     * 用蛮力法及回溯法找出假币；把假币分成四份，能比较三次，只要第二次能确
     * 定某三个或三个以下的硬币中有假币，最后一次必定能找到假币
     *
     * @return Coin (假币对象)
     */
    public Coin FindFakeCoin() {
        //把硬币分成三份，先比较第一份和第二份；
        if (weight(0, 1, 2, 3) == weight(4, 5, 6, 7)) {
            System.out.println("ABCD = EFGH");
            totalString+="ABCD = EFGH<br>";
            if (weight(0, 1, 2, 8) > weight(4, 5, 9, 10)) {
                System.out.println("ABCI > EFJK");
                totalString += "ABCI > EFJK<br>";
                if (weight(0, 1, 8, 9) > weight(4, 5, 6, 7)) {
                    System.out.println("ABIJ > EFGH");
                    totalString+= "ABIJ > EFGH<br>";
                    return coins.get(8);
                } else if (weight(0, 1, 8, 9) < weight(4, 5, 6, 7)) {
                    System.out.println("ABIJ < EFGH");
                    totalString+="ABIJ < EFGH<br>";
                    return coins.get(9);
                } else {
                    System.out.println("ABIJ = EFGH");
                    totalString+="ABIJ = EFGH<br>";
                    return coins.get(10);
                }
            } else if (weight(0, 1, 2, 8) < weight(4, 5, 9, 10)) {
                System.out.println("ABCI < EFJK");
                totalString+="ABCI < EFJK<br>";
                if (weight(8, 9) < weight(0, 1)) {
                    System.out.println("IJ < AB");
                    totalString+="IJ < AB<br>";
                    return coins.get(8);
                } else if (weight(8, 9) > weight(0, 1)) {
                    System.out.println("IJ > AB");
                    totalString+="IJ > AB<br>";
                    return coins.get(9);
                } else {
                    System.out.println("IJ = AB");
                    totalString+="IJ = AB<br>";
                    return coins.get(10);
                }
            } else {
                System.out.println("ABCI = EFJK");
                totalString+="ABCI = EFJK<br>";
                return coins.get(11);
            }
        } else if (weight(0, 1, 2, 3) > weight(4, 5, 6, 7)) {
            System.out.println("ABCD > EFGH");
            totalString+="ABCD > EFGH<br>";
            if (weight(0, 1, 4) > weight(2, 3, 8)) {
                System.out.println("ABE > CDI");
                totalString+="ABE > CDI<br>";
                if (weight(0) > weight(1)) {
                    System.out.println("A > B");
                    totalString+="A > B<br>";
                    return coins.get(0);
                } else {
                    System.out.println("A < B");
                    totalString+="A < B<br>";
                    return coins.get(1);
                }
            } else if (weight(0, 1, 4) < weight(2, 3, 8)) {
                System.out.println("ABE < CDI");
                totalString+="ABE < CDI<br>";
                if (weight(2) < weight(3)) {
                    System.out.println("C < D");
                    totalString+="C < D";
                    return coins.get(3);
                } else {
                    System.out.println("C > D");
                    totalString+="C > D<br>";
                    return coins.get(2);
                }
            } else {
                System.out.println("ABE = CDI");
                totalString+="ABE = CDI<br>";
                if (weight(5) < weight(6)) {
                    System.out.println("F < G");
                    totalString+="F < G<br>";
                    return coins.get(5);
                } else if (weight(5) > weight(6)) {
                    System.out.println("F > G");
                    totalString+="F > G<br>";
                    return coins.get(6);
                } else {
                    System.out.println("F = G");
                    totalString+="F = G<br>";
                    return coins.get(7);
                }
            }
        } else {
            System.out.println("ABCD < EFGH");
            totalString+="ABCD < EFGH<br>";
            if (weight(0, 1, 4) > weight(2, 3, 8)) {
                System.out.println("ABE > CDI");
                totalString+="ABE > CDI<br>";
                if (weight(2, 4) < weight(8, 9)) {
                    System.out.println("CE < IJ");
                    totalString+="CE < IJ<br>";
                    return coins.get(2);
                } else if (weight(2, 4) > weight(8, 9)) {
                    System.out.println("CE > IJ");
                    totalString+="CE > IJ<br>";
                    return coins.get(4);
                } else {
                    System.out.println("CE = IJ");
                    totalString+="CE = IJ<br>";
                    return coins.get(3);
                }
            } else if (weight(0, 1, 4) < weight(2, 3, 8)) {
                System.out.println("ABE < CDI");
                totalString+="ABE < CDI<br>";
                if (weight(0) < weight(1)) {
                    System.out.println("A < B");
                    totalString+="A < B<br>";
                    return coins.get(0);
                } else {
                    System.out.println("A > B");
                    totalString+="A > B<br>";
                    return coins.get(1);
                }
            } else {
                System.out.println("ABE = CDI");
                totalString+="ABE = CDI<br>";
                if (weight(5) < weight(6)) {
                    System.out.println("F < G");
                    totalString+="F < G<br>";
                    return coins.get(6);
                } else if (weight(5) > weight(6)) {
                    System.out.println("F > G");
                    totalString+="F > G<br>";
                    return coins.get(5);
                } else {
                    System.out.println("F = G");
                    totalString+="F = G<br>";
                    return coins.get(7);
                }
            }
        }
    }

//    public static void main(String[] args) {
//        Experiment5 experiment = new Experiment5();
//        System.out.println("假币为：" + experiment.FindFakeCoin());
//

    public String runMain(){
        totalString +="假币为：" + FindFakeCoin()+"<br>";
        return totalString;
    }

}

class Coin {
    String name;
    int weight = 2;

    @Override
    public String toString() {
        return '{' + name +
                " , " + weight +
                '}';
    }
}

